package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService{
	@Autowired
	private CustomerMapper customerMapper;
	@Override
	public void insert(Customer customer) {
		customer.setInputTime(new Date());
		customer.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
		customer.setStatus(0);
		customerMapper.insert(customer);
	}

	@Override
	public void delete(Long id) {
		customerMapper.delete(id);
	}

	@Override
	public void update(Customer customer) {
		customerMapper.update(customer);
	}

	@Override
	public Customer select(Long id) {
		Customer customer = customerMapper.select(id);
		return customer;
	}

	@Override
	public List<Customer> list() {
		List<Customer> list = customerMapper.list();
		return list;
	}

	public PageInfo<Customer> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> customers = customerMapper.selectForList(qo);
        return new PageInfo<>(customers);
	}

	public void updateStatus(Long customerId, int status) {
		customerMapper.updateStatus(customerId,status);
	}

	public void updateSeller(Long customerId, Long newSellerId) {
		customerMapper.updateSeller(customerId,newSellerId);
	}

	public void updateStatusAndSellerId(Long customerId, int status, Long newSellerId) {
		customerMapper.updateStatus(customerId,status);
		customerMapper.updateSeller(customerId,newSellerId);
	}

	public List<Customer> selectByStatus(int status) {
		return customerMapper.selectByStatus(status);
	}


}
